#include <iostream>
#include <vector>
#include <stack>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
    struct tree_node_info {
        TreeNode* node;
        uint8_t info;
    };
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<tree_node_info> stack;
        vector<int> result;
        if (root == NULL) {
            return {};
        }
        begin_1:
        while (root->left != NULL) {
            stack.push({root, 0});
            root = root->left;
        }
        begin_2:
        if (root->right != NULL) {
            stack.push({root, 1});
            root = root->right;
            goto begin_1;
        }
        begin_3:
        result.push_back(root->val);
        if (stack.size() == 0) {
            return result;
        }
        tree_node_info stack_top = stack.top();
        stack.pop();
        root = stack_top.node;
        if (stack_top.info == 0) {
            goto begin_2;
        } else {
            goto begin_3;
        }
    }
};


int main() {
    TreeNode t1(3), t2(1), t3(2);
    t1.left = &t2;
    t1.right = &t3;
    Solution solution;
    vector<int> out = solution.postorderTraversal(&t1);
    for (int o: out) {
        cout << o << ",";
    }
}